package com.slkj.shilixiaoyuanapp.ui.teacher.tool.MaterialAudit;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.example.api.InjectParam;
import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.base.ActivityInfo;
import com.slkj.shilixiaoyuanapp.base.BaseActivity;
import com.slkj.shilixiaoyuanapp.entity.parent.AskLeaveRecordEntity;
import com.slkj.shilixiaoyuanapp.net.HttpHeper;
import com.slkj.shilixiaoyuanapp.net.callback.CommonCallBack;
import com.slkj.shilixiaoyuanapp.ui.common.adapter.ChosePicAdapter;
import com.slkj.shilixiaoyuanapp.view.NumberEditText;
import com.slkj.shilixiaoyuanapp.view.dialog.LoadSuccessAndFailDialog;

import butterknife.BindView;
import butterknife.OnClick;

import static com.slkj.shilixiaoyuanapp.entity.code.UiCode.CHOSE_PIC_SPAN_COUNT;

@ActivityInfo(layout = R.layout.activity_sh_student_leave,title = "请假")
public class ShStudentLeaveActivity extends BaseActivity {
    @InjectParam
    int aId;
    @InjectParam
    int id;
    @InjectParam
    AskLeaveRecordEntity data;
    @BindView(R.id.recyc_pic)
    RecyclerView recycerPic;
    @BindView(R.id.tv_leavetype)
    TextView tvType;
    @BindView(R.id.tv_start_time)
    TextView tvStartTime;
    @BindView(R.id.tv_end_time)
    TextView tvEndTime;
    @BindView(R.id.edit_day)
    TextView editDay;
    @BindView(R.id.edit_content)
    NumberEditText editContent;
    @BindView(R.id.tv_student_name)
    TextView tvUserName;
    ChosePicAdapter adapter;
    @BindView(R.id.layout_botton)
    View viewBotton;
    @BindView(R.id.layout_reson)
    View viewReson;
    @BindView(R.id.edit_reson)
    EditText editReson;
    @Override
    protected void init() {
        ShStudentLeaveActivity__JumpCenter.bind(this);
        tvUserName.setText(data.getName());
        if (data.getType()==1){
            tvType.setText("事假");
        }else {
            tvType.setText("病假");
        }
        tvStartTime.setText(data.getStartDate());
        tvEndTime.setText(data.getEndDate());
        editDay.setText(String.valueOf(data.getDuration()));
        editContent.setText(data.getBody());
        if (data.getImg()!= null && !data.getImg().isEmpty()){
            recycerPic.setLayoutManager(new GridLayoutManager(this, CHOSE_PIC_SPAN_COUNT));
            adapter = new ChosePicAdapter(data.getImg(), this,false);
            recycerPic.setAdapter(adapter);
        }
        switch (data.getState()){
            case MaterialAuditHistoryFragment.TYPE_NOT_LOOK:
                viewBotton.setVisibility(View.VISIBLE);

                break;
            case MaterialAuditHistoryFragment.TYPE_REFUSE:
                viewBotton.setVisibility(View.GONE);
                editReson.setFocusable(false);
                break;
            case MaterialAuditHistoryFragment.TYPE_OK:
                viewBotton.setVisibility(View.GONE);
                editReson.setFocusable(false);
                break;
        }
        editReson.setText(data.getRefute());
        viewBotton.setVisibility(data.getMyState() == MaterialAuditHistoryFragment.TYPE_NOT_LOOK?View.VISIBLE:View.GONE);
    }
    @OnClick(R.id.tv_tg)void tg(){
        String reson = editReson.getText().toString();
        upData(reson,2);

    }

    private void upData(String reson,int state) {
        HttpHeper.get().toolService()
                .materialReview(aId,id,state,reson)
                .compose(getThread())
                .compose(bindToLifecycle())
                .subscribe(new CommonCallBack<String>(true, this) {
                    @Override
                    public void onCallBackSuccess(String data) {
                        //showToast(data);
                        LoadSuccessAndFailDialog.showSuccess(ShStudentLeaveActivity.this,data);
                        finish();
                    }
                });
    }

    @OnClick(R.id.tv_wtg)void wtg(){
        String reson = editReson.getText().toString();
        if (TextUtils.isEmpty(reson)){
            showToast("请填写拒绝原因");
            return;
        }
        upData(reson,1);
    }
}
